package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.core.common.util.DateUtils;
import com.homedo.odin.solution.dao.CommonTypeDataMapper;
import com.homedo.odin.solution.dao.HundredStylistInfoMapper;
import com.homedo.odin.solution.dao.HundredStylistSolutionApplyInfoMapper;
import com.homedo.odin.solution.dao.SolutionMapper;
import com.homedo.odin.solution.enumcode.PublicDeleteEnum;
import com.homedo.odin.solution.enumcode.activity.HundredStylistInfoAuditStatus;
import com.homedo.odin.solution.enumcode.activity.HundredStylistSaveStatus;
import com.homedo.odin.solution.model.entity.CommonTypeData;
import com.homedo.odin.solution.model.entity.HundredStylistInfo;
import com.homedo.odin.solution.model.entity.HundredStylistSolutionApplyInfo;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.request.activity.HundredStylistInfoRequestBean;
import com.homedo.odin.solution.model.request.mng.MngHundredStylistInfoListRequestBean;
import com.homedo.odin.solution.model.request.mng.MngHundredStylistInfoUpdateRequestBean;
import com.homedo.odin.solution.model.response.activity.HundredSolutionResponseBean;
import com.homedo.odin.solution.model.response.activity.HundredStylistApplyTableResponseBean;
import com.homedo.odin.solution.model.response.activity.HundredStylistElectPageResponseBean;
import com.homedo.odin.solution.model.response.activity.HundredStylistElectResponseBean;
import com.homedo.odin.solution.model.response.activity.HundredStylistInfoResponseBean;
import com.homedo.odin.solution.model.response.activity.SolutionStylistInfoResponseBean;
import com.homedo.odin.solution.model.response.mng.MngHundredStylistInfoDetailResponseBean;
import com.homedo.odin.solution.model.response.mng.MngHundredStylistPageResponseBean;
import com.homedo.odin.solution.model.response.mng.MngHundredStylistResponseBean;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhanglei
 * @date: 2018/4/25
 * @Description:
 */
@Service
public class HundredStylistInfoServiceImpl extends BaseService<HundredStylistInfoMapper, HundredStylistInfo> {

    @Autowired
    private HundredStylistSolutionApplyInfoMapper hundredStylistSolutionApplyInfoDao;

    @Autowired
    private SolutionMapper solutionDao;

    @Autowired
    private CommonTypeDataMapper commonTypeDataDao;

    /**
     * Mng百名设计师评选列表查询
     */
    public MngHundredStylistPageResponseBean findStylistInfoPageList(MngHundredStylistInfoListRequestBean requestBean) {
        //获取db数据并转换对象
        PageInfo<HundredStylistInfo> pageInfo = PageHelper
                .startPage(requestBean.getPageNum(), requestBean.getPageSize())
                .doSelectPageInfo(() -> baseMapper
                        .findStylistInfoPageList(requestBean.getAreaBranch(), requestBean.getAuditStatus(),
                                requestBean.getCompany(), requestBean.getStylistName(), requestBean.getReferrer()));

        List<HundredStylistInfo> dbList = pageInfo.getList();
        List<MngHundredStylistResponseBean> collect = dbList.stream()
                .filter(info -> info != null)
                .map(info -> transfrom(info))
                .collect(Collectors.toList());

        MngHundredStylistPageResponseBean responseBean = BeanUtils
                .transfrom(MngHundredStylistPageResponseBean.class, pageInfo);
        responseBean.setList(collect);
        return responseBean;
    }


    /**
     * Mng百名设计师评选详情
     */
    public MngHundredStylistInfoDetailResponseBean findStylistInfoByUserId(Integer userId) {
        //获取设计师填写的基本信息
        HundredStylistInfo hundredStylistInfo = getHundredStylistInfo(userId);

        MngHundredStylistInfoDetailResponseBean responseBean = BeanUtils
                .transfrom(MngHundredStylistInfoDetailResponseBean.class, hundredStylistInfo);

        //草稿保存直接返回，因为没有提交过评选方案
        if (HundredStylistSaveStatus.SAVE.equals(hundredStylistInfo.getStatus())) {
            return responseBean;
        }

        //参评方案与设计师申报表地址  中记录的设计师方案ID
        HundredStylistSolutionApplyInfo applyInfo = hundredStylistSolutionApplyInfoDao.findByUserId(userId);
        if (applyInfo == null) {
            return responseBean;
        }

        //设计师上传的申请表附件地址
        responseBean.setStylistInfoTableAddress(applyInfo.getStylistInfoTableAddress());
        //设计师参选信息表名称
        responseBean.setStylistInfoTableName(applyInfo.getStylistInfoTableName());

        //获取设计师申请附件方案表的方案ID
        Long solutionId = applyInfo.getSolutionId();
        if (solutionId == null) {
            throw new BizException(Meta.CODE_451.getCode(), "百名设计师活动，设计师评选方案申报表中的solutionId不存在");
        }

        //获取设计师评选选择的方案
        Solution dbSolution = getSolution(solutionId, userId, PublicDeleteEnum.NOT_DEL.getDbValue());
        //方案ID
        responseBean.setSolutionId(dbSolution.getSolutionId());
        //方案名
        responseBean.setSolutionName(dbSolution.getSolutionName());
        //方案附件地址
        responseBean.setAttachment(dbSolution.getAttachment());
        return responseBean;
    }


    /**
     * Mng百名设计师详情修改
     */
    public void updateStylistInfoDetail(MngHundredStylistInfoUpdateRequestBean requestBean) {
        //获取设计师填写的基本信息
        HundredStylistInfo hundredStylistInfo = getHundredStylistInfo(requestBean.getUserId());

        //拷贝修改过的数据到dbData进行修改
        org.springframework.beans.BeanUtils.copyProperties(requestBean, hundredStylistInfo);
        hundredStylistInfo.setLastModifiedTime(new Date());
        baseMapper.updateById(hundredStylistInfo);
    }


    /**
     * Mng百名设计师评选审核
     */
    public void updateStylistInfoAudit(Integer userId, Integer auditStatus) {
        HundredStylistInfo hundredStylistInfo = getHundredStylistInfo(userId);
        hundredStylistInfo.setAuditStatus(auditStatus);
        hundredStylistInfo.setLastModifiedTime(new Date());
        baseMapper.updateById(hundredStylistInfo);
    }


    /**
     * PC百名设计师评选  信息录入
     */
    public void stylistInfo(HundredStylistInfoRequestBean requestBean) {
        //设计师是否提交过资料，提交过修改数据信息，userId校验类必传正数
        Integer userId = requestBean.getUserId();
        HundredStylistInfo dbInfo = baseMapper.selectByUserId(userId);
        HundredStylistInfo transfrom = BeanUtils.transfrom(HundredStylistInfo.class, requestBean);

        transfrom.setAuditStatus(HundredStylistInfoAuditStatus.AUDIT_WAIT.getDbValue());
        if (dbInfo == null) {
            baseMapper.insert(transfrom);
            return;
        }

        //已有资料做修改
        transfrom.setId(dbInfo.getId());
        transfrom.setLastModifiedTime(new Date());
        baseMapper.updateById(transfrom);
    }


    /**
     * PC百名设计师评选  获取用户评选信息
     */
    public SolutionStylistInfoResponseBean querySolutionStylistInfo(Integer userId) {
        //获取设计师填写的基本信息
        HundredStylistInfo hundredStylistInfo = getHundredStylistInfo(userId);

        SolutionStylistInfoResponseBean responseBean = new SolutionStylistInfoResponseBean();
        HundredStylistInfoResponseBean stylistInfo = BeanUtils
                .transfrom(HundredStylistInfoResponseBean.class, hundredStylistInfo);
        responseBean.setHundredStylistInfo(stylistInfo);

        //草稿保存直接返回，因为没有提交过评选方案
        if (HundredStylistSaveStatus.SAVE.equals(hundredStylistInfo.getStatus())) {
            return responseBean;
        }

        //参评方案与设计师申报表地址  中记录的设计师方案ID
        HundredStylistSolutionApplyInfo applyInfo = hundredStylistSolutionApplyInfoDao.findByUserId(userId);
        if (applyInfo == null) {
            return responseBean;
        }

        //参选的申报表信息
        HundredStylistApplyTableResponseBean applyTableResponseBean = BeanUtils
                .transfrom(HundredStylistApplyTableResponseBean.class, applyInfo);
        responseBean.setApplyTableResponseBean(applyTableResponseBean);

        //获取设计师申请附件方案表的方案ID
        Long solutionId = applyInfo.getSolutionId();
        if (solutionId == null) {
            throw new BizException(Meta.CODE_451.getCode(), "百名设计师活动，设计师评选方案申报表中的solutionId不存在");
        }

        //获取设计师评选选择的方案
        Solution dbSolution = getSolution(solutionId, userId, PublicDeleteEnum.NOT_DEL.getDbValue());
        HundredSolutionResponseBean solutionResponseBean = BeanUtils
                .transfrom(HundredSolutionResponseBean.class, dbSolution);
        String buildingType = dbSolution.getBuildingType();
        if (StringUtils.isBlank(buildingType) || buildingType.split(",").length == 0) {
            solutionResponseBean.setBuildingType("");
        } else {
            String[] buildingTypeArr = buildingType.split(",");
            List<CommonTypeData> commonTypeData = commonTypeDataDao.selectByParentIdAndIds(buildingTypeArr);
            if (CollectionUtils.isEmpty(commonTypeData)) {
                solutionResponseBean.setBuildingType("");
            } else {
                String joinName = commonTypeData.stream().filter(data -> data != null).map(data -> data.getName())
                        .collect(Collectors.joining(","));
                solutionResponseBean.setBuildingType(joinName);
            }
        }

        responseBean.setSolution(solutionResponseBean);

        return responseBean;
    }


    /**
     * PC_百名设计师评选结果接口
     */
    public HundredStylistElectPageResponseBean querySolutionStylistElect(Integer pageNum, Integer pageSize) {

        PageInfo<HundredStylistElectResponseBean> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> baseMapper.selectElectResult());

        List<HundredStylistElectResponseBean> list = pageInfo.getList();
        if (CollectionUtils.isEmpty(list)) {
            throw new BizException(Meta.CODE_451.getCode(), "百名设计师活动，没有评选结果");
        }

        HundredStylistElectPageResponseBean responseBean = BeanUtils
                .transfrom(HundredStylistElectPageResponseBean.class, pageInfo);
        return responseBean;
    }

    /**
     * 获取方案信息
     */
    private Solution getSolution(Long solutionId, Integer userId, Integer isDel) {
        Solution dbSolution = solutionDao.findBySolutionIdAndDelAndStatus(solutionId, userId, isDel);
        if (dbSolution == null) {
            throw new BizException(Meta.CODE_451.getCode(), "百名设计师活动，该设计师的评选方案不存在");
        }
        return dbSolution;
    }


    /**
     * 查询设计师资料
     */
    private HundredStylistInfo getHundredStylistInfo(Integer userId) {
        HundredStylistInfo hundredStylistInfo = baseMapper.selectByUserId(userId);
        if (hundredStylistInfo == null) {
            throw new BizException(Meta.CODE_451.getCode(), "百名设计师活动，该设计师没有填写资料");
        }
        return hundredStylistInfo;
    }


    /**
     * 对象转换
     */
    private MngHundredStylistResponseBean transfrom(HundredStylistInfo info) {
        MngHundredStylistResponseBean transfrom = BeanUtils.transfrom(MngHundredStylistResponseBean.class, info);
        String str = DateUtils.formatDate(info.getCreateTime(), "yyyy-MM-dd hh:mm:ss");
        transfrom.setCreateTime(str);
        return transfrom;
    }


}
